package org.zxstudio.encrypt.provider;

import org.zxstudio.encrypt.utils.RLog;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

/**
 * 
 * @author fan
 * 
 */
public class EncryptProvider extends ContentProvider {

	private static final String TAG = "EncryptProvider";
	private static final boolean DEBUG = true;

	private SQLiteOpenHelper mDbHelper = null;
	public static final String ENCRYPT_TYPE = "vnd.android.cursor.dir/encrypt-file";
	private static final String DATABASE_NAME = "encrypt.db";
	private static final int DATABASE_VERSION = 1;

	public static final String TABLE_ENCRYPT = "encrypt";
	private static final String AUTHORITY = "org.zxstudio.encryptfile";

	public static final String NAME = "name";
	public static final String STUB_NAME = "stub_name";
	public static final String PATH = "path";
	public static final String STUB_PATH = "stub_path";
	public static final String THUMBNAILS = "thumbnails";
	public static final String FILE_TYPE = "file_type";
	
	

	public static final Uri ENCRYPT_URI = Uri
			.parse("content://org.zxstudio.encryptfile/encrypt");
	
	public static final String[] sPROJECTIONS = new String[] { "_id", "name",
		"stub_name", "path", "stub_path", "thumbnails", "file_type" };
	private static final UriMatcher sUriMatcher = new UriMatcher(
			UriMatcher.NO_MATCH);
	private static final int ENCRYPT_FILE_ALL = 0;
	private static final int ENCRYPT_FILE_ID = 1;
	static {
		sUriMatcher.addURI(AUTHORITY, "encrypt", ENCRYPT_FILE_ALL);
		sUriMatcher.addURI(AUTHORITY, "encrypt/*", ENCRYPT_FILE_ID);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case ENCRYPT_FILE_ALL:
			count = db.delete(TABLE_ENCRYPT, selection, selectionArgs);
			this.getContext().getContentResolver().notifyChange(ENCRYPT_URI, null);
			break;
		case ENCRYPT_FILE_ID:
			long id = ContentUris.parseId(uri);
			String where = "_id= " + id;
			where += !TextUtils.isEmpty(selection) ? " and (" + selection
					+ ") " : "";
			count = db.delete(TABLE_ENCRYPT, where, selectionArgs);
			this.getContext().getContentResolver().notifyChange(ENCRYPT_URI, null);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long id = 0;
		switch (sUriMatcher.match(uri)) {
		case ENCRYPT_FILE_ALL:
			RLog.v(TAG, "insert values into table encrypt");
			id = db.insert(TABLE_ENCRYPT, "stub_name", values);
			return ContentUris.withAppendedId(uri, id);
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public boolean onCreate() {
		if (DEBUG) {
			Log.v(TAG, "onCreate");
		}
		mDbHelper = new EncryptDatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		switch (sUriMatcher.match(uri)) {
		case ENCRYPT_FILE_ALL:
			return db.query(TABLE_ENCRYPT, projection, selection,
					selectionArgs, null, null, sortOrder);
		case ENCRYPT_FILE_ID:
			long id = ContentUris.parseId(uri);
			String where = "_id = " + id;
			where += !TextUtils.isEmpty(selection) ? " and ( " + selection
					+ ")" : "";
			return db.query(TABLE_ENCRYPT, projection, where, selectionArgs,
					null, null, sortOrder);
		default:            
			throw new IllegalArgumentException("Unknown URI " + uri);   
		}
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		int count = 0;
		switch (sUriMatcher.match(uri)) {
		case ENCRYPT_FILE_ALL:
			count = db.update(TABLE_ENCRYPT, values, selection, selectionArgs);
			break;
		case ENCRYPT_FILE_ID:
			long id = ContentUris.parseId(uri);
			String where = "_id = " + id;
			where += !TextUtils.isEmpty(selection) ? " and (" + selection + ")"
					: "";
			count = db.update(TABLE_ENCRYPT, values, where, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
		db.close();
		return count;
	}

	private class EncryptDatabaseHelper extends SQLiteOpenHelper {

		public EncryptDatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// createUserTable(db);
			createEncryptTable(db);
		}

		private void createEncryptTable(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE encrypt ("
					+ "_id INTEGER PRIMARY KEY autoincrement," + "name TEXT,"
					+ "stub_name  TEXT, " + "path TEXT, " + "stub_path TEXT ,"
					+ "thumbnails  BLOB," + "file_type INTEGER " + ");" + ");");
		}

		private void createUserTable(SQLiteDatabase db) {
			db.execSQL("CREATE TABLE encrypt ("
					+ "_id INTEGER PRIMARY KEY autoincrement,"
					+ "sourcePath  TEXT," + "destPath TEXT ,"
					+ "filetype INTEGER DEFAULT 0" + ");");
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

		}

	}

}
